<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="姓名" prop="populationName">
        <el-input
          v-model="queryParams.populationName"
          placeholder="请输入姓名"
          clearable
        />
      </el-form-item>
      <el-form-item label="证件号" prop="idCardNum">
        <el-input
          v-model="queryParams.idCardNumber"
          placeholder="请输入证件号"
          clearable
        />
      </el-form-item>
<!--      <el-form-item label="任务等级" prop="level">-->
<!--        <el-input-->
<!--          v-model="queryParams.level"-->
<!--          placeholder="请输入任务等级"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="发布人" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入发布人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="任务描述" prop="desc">-->
<!--        <el-input-->
<!--          v-model="queryParams.desc"-->
<!--          placeholder="请输入任务描述"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['deploy:face:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['deploy:face:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div>
      <el-row>
        <el-col>
          <h3>当前人脸布控任务数量: {{ total }}</h3>
        </el-col>
        <el-col>
          <el-button type="primary" @click="handlePauseOrStart(-1, 1)">一键开启</el-button>
          <el-button type="primary" @click="handlePauseOrStart(-1, 2)">一键暂停</el-button>
        </el-col>
        <el-col>
          <el-button type="primary" @click="getList(-1)">实有人口布控</el-button>
          <el-button type="primary" @click="getList">全部布控</el-button>
        </el-col>
      </el-row>

    </div>


    <el-table
      v-loading="loading"
      :data="captureData"
      @selection-change="handleSelectionChange"
      :default-sort="{prop: 'createTime', order: 'descending'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="布控照片" align="center" prop="taskImages">
        <template #default="scope">
          <image-preview :src="scope.row.taskImages" width="120px" height="120px"/>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        sortable
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="dict.type.deploy_task_status" :value="scope.row.status"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="任务等级" align="center" prop="level" />
      <el-table-column label="发布人" align="center" prop="userName" />
      <el-table-column label="任务描述" align="center" prop="taskDesc" />
      <el-table-column label="任务类型" align="center" prop="taskType">
        <template #default="scope">
          <dict-tag :options="dict.type.deploy_task_type" :value="scope.row.taskType"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['deploy:face:edit']"
            :disabled="scope.row.status !== 0 && scope.row.status !== 2"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePauseOrStart(scope.row.id, scope.row.status === 1 ? 2 : 1)"
            :disabled="scope.row.status === 3 || scope.row.status === -1 "
          >暂停/开始</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handlePauseOrStart(scope.row.id, -1)"
            v-hasPermi="['deploy:face:remove']"
            :disabled="scope.row.status !== 0 && scope.row.status !== 2 && scope.row.status !== 3"
          >撤销</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 1)"
            v-if="scope.row.status === -1"
          >重新布控</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleInfo(scope.row.id)"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改抓拍任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" :rules="rules">
      <el-form ref="form" :model="deployForm" :rules="rules" label-width="80px">
        <el-form-item label="布控照片" prop="taskImages">
          <ImageUpload v-model="deployForm.taskImages"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="deployForm.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="deployForm.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务等级" prop="level">
          <el-input type="number" v-model="deployForm.level" placeholder="请输入任务等级" />
        </el-form-item>
        <el-form-item label="相似度设置" prop="similarity">
          <el-slider v-model="deployForm.similarity"></el-slider>
        </el-form-item>
        <el-form-item label="任务描述" prop="desc">
          <el-input v-model="deployForm.taskDesc" placeholder="请输入任务描述" />
        </el-form-item>
        <el-form-item label="任务类型" prop="taskType">
            <el-radio-group v-model="deployForm.taskType">
              <el-radio
                v-for="dict in dict.type.deploy_task_type"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--任务详情弹窗-->
    <el-dialog title="任务详情" :visible.sync="infoDialog" width="500px" append-to-body >
      <div v-if="faceInfo">
        <el-card>
          <div class="item" v-if="faceInfo.populationName!== null && faceInfo.populationName !== ''">
            <span>姓名:</span>
            <span>{{ faceInfo.populationName }}</span>
          </div>
          <div class="item" v-if="faceInfo.idCardNumber !== null && faceInfo.idCardNumber !== ''">
            <span>证件号:</span>
            <span>{{ faceInfo.idCardNumber }}</span>
          </div>
          <div class="item">
            <span>任务描述:</span>
            <span>{{ faceInfo.taskDesc }}</span>
          </div>
          <div class="item">
            <span>任务类型:</span>
            <dict-tag :options="dict.type.deploy_task_type" :value="faceInfo.taskType"></dict-tag>
          </div>
          <div class="item">
            <span>任务状态:</span>
            <dict-tag :options="dict.type.deploy_task_status" :value="faceInfo.status"></dict-tag>
          </div>
          <div class="item">
            <span>任务等级:</span>
            <span>{{ faceInfo.level }}</span>
          </div>
          <div class="item">
            <span>相似度:</span>
            <span>{{ faceInfo.similarity }}% </span>
          </div>
          <div class="item">
            <span>开始时间:</span>
            <span>{{ faceInfo.startTime }}</span>
          </div>
          <div class="item">
            <span>结束时间:</span>
            <span>{{ faceInfo.endTime }}</span>
          </div>
          <div class="item">
            <span>告警数量:</span>
            <span>{{ faceInfo.alarmNum }}</span>
          </div>
          <div class="item">
            <span>告警详情:</span>
            <span>
              <el-button @click="navigateToPage(faceInfo.id)">点击查看</el-button>
            </span>
          </div>
        </el-card>
      </div>
      <div>
        <el-button @click="infoDialog = 0">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listFace, getFace, delFace, addFace, updateFace } from "@/api/deploy/face";
import ImageUpload from '@/components/ImageUpload'
import {getTaskFaceDetail, pauseOrStart} from "../../../api/deploy/face";
export default {
  name: "Face",
  dicts: ['deploy_task_type','deploy_task_status'],
  components: { ImageUpload },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 抓拍任务表格数据
      captureData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        subject: null,
        taskImages: null,
        endTime: null,
        status: null,
        level: null,
        userId: null,
        desc: null,
        taskType: null
      },
      // 表单参数
      deployForm: {},
      // 表单校验
      rules: {
        taskImages: [
          { required: true, message: '请上传布控照片', trigger: 'change' }
        ],
        startTime: [
          { required: true, message: '请选择开始时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请选择结束时间', trigger: 'change' }
        ],
        level: [
          { required: true, message: '请输入任务等级', trigger: 'blur' }
        ],
        similarity: [
          { required: true, message: '请设置相似度', trigger: 'change' }
        ],
        taskDesc: [
          { required: true, message: '请输入任务描述', trigger: 'blur' }
        ],
        taskType: [
          { required: true, message: '请选择任务类型', trigger: 'change' }
        ]
      },
      infoDialog: false,
      faceInfo: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 跳转告警列表页面
    navigateToPage(id) {
      this.$router.push({path: 'alarm', query: {taskId: id}})
    },
    // 查看详情
    handleInfo(id) {
      getTaskFaceDetail(id).then(response => {
        this.faceInfo = response.data;
        this.infoDialog = true;
      })
    },
    // 暂停/开始
    handlePauseOrStart(id, status) {
      this.loading = true;
      let subject = 'face,'+id;
      pauseOrStart(subject, id, status).then(response => {
        if (response.code === 200){
          this.$modal.msgSuccess("操作成功");
          this.getList();
          this.loading = false;
          window.location.reload()
        }
      })
    },
    /** 查询抓拍任务列表 */
    getList(flag) {
      let flag_ = 0
      if (flag === -1){
        flag_ = flag;
      }
      this.loading = true;
      listFace(this.queryParams, flag_).then(response => {
        this.captureData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.deployForm = {
        id: null,
        subject: null,
        taskImages: null,
        startTime: null,
        endTime: null,
        status: null,
        level: null,
        userId: null,
        desc: null,
        taskType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加抓拍任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row, status) {
      this.reset();
      const id = row.id || this.ids
      getFace(id).then(response => {
        this.deployForm = response.data;
        this.deployForm.taskType = response.data.taskType.toString();
        if (status){
          this.deployForm.status = status
        }
        this.open = true;
        this.title = "修改抓拍任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.deployForm.id != null) {
            updateFace(this.deployForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFace(this.deployForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认撤销抓拍任务编号为"' + ids + '"的数据项？').then(function() {
        return delFace(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('deploy/face/export', {
        ...this.queryParams
      }, `face_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
</style>
